﻿using Newtonsoft.Json;

namespace Server
{
    public class ResHeartBeat
    {
        private static int uid = 0;
        
        [JsonIgnore]
        public uint packetLength;
        [JsonIgnore]
        public byte operationType;
        [JsonIgnore]
        public byte zipFlag;
        [JsonIgnore]
        public int uniqueId;
        [JsonIgnore]
        public int id = 655371;
        public long timestamp;

        public ResHeartBeat()
        {
            uid++;
            packetLength = sizeof(uint) + sizeof(byte) + sizeof(byte) + sizeof(int) + sizeof(int);
            operationType = 1;
            zipFlag = 0;
            uniqueId = uid;
            id = 655371;
            timestamp = 123;
        }
        
        public byte[] Serialize()
        {
            var offset = 0;
            var data = JsonSerializerHelper.Serialize<ResHeartBeat>(this);
            packetLength += (uint)data.Length;

            var buffer = new byte[packetLength];
            buffer.WriteUInt(packetLength, ref offset);
            buffer.WriteByte(operationType, ref offset);
            buffer.WriteByte(zipFlag, ref offset);
            buffer.WriteInt(uniqueId, ref offset);
            buffer.WriteInt(id, ref offset);
            buffer.WriteBytesWithoutLength(data, ref offset);
            return buffer;
        }
    }
}